//
//  MYXLabelViewController.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/11/7.
//  Copyright © 2017年 hbjApple. All rights reserved.
//标签界面

#import "MYXLabelViewController.h"
#import "XLChannelView.h"
#import "MYXInterestViewController.h"
#import "MYXMemberMdel.h"
#import "userinfoModel.h"


@interface MYXLabelViewController (){
     XLChannelView *_channelView;
}
@property(nonatomic,strong)MYXHttpManager *MYXHttpManag;
@property (nonatomic, strong) NSMutableArray *randtagsArr;
@property (nonatomic, strong) NSMutableArray *LabelArr;
@property (nonatomic, strong)userinfoModel *userinfoMode;
@end

@implementation MYXLabelViewController


-(NSMutableArray *)randtagsArr
{
    if (!_randtagsArr) {
        _randtagsArr = [NSMutableArray array];
    }
    
    return _randtagsArr;
}

-(NSMutableArray *)LabelArr
{
    if (!_LabelArr) {
        _LabelArr = [NSMutableArray array];
    }
    
    return _LabelArr;
}
-(void)setMYXMemberMd:(MYXMemberMdel *)MYXMemberMd{
    _MYXMemberMd = MYXMemberMd;
    self.userinfoMode =[[userinfoModel alloc]init];
    self.userinfoMode = MYXMemberMd.userinfo;
   
    
}

-(void)right
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"确定" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(sure) forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(0, 0, 60, 44);
    UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    [self.navigationItem setRightBarButtonItem:cancelItem animated:NO];
    
}
-(void)sure{
    NSString *string;
    if (_channelView.inUseTitles.count>0) {
        if (_channelView.inUseTitles.count > 5) {
            [self showNoReceivertext:@"最多只能选择5个标签"];
            return;
        }
         string = [_channelView.inUseTitles componentsJoinedByString:@","];
        
    }else {
        string = @"";
        //[self showNoReceivertext:@"请选择标签"];
        //return;
    }
    
    //为了防止键盘的闪屏，延迟0.25秒退出
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.LabelBlock) {
             [[NSNotificationCenter defaultCenter] postNotificationName:@"info" object:nil];
            self.LabelBlock(string);
        }
        
        [self.navigationController popViewControllerAnimated:YES];
        
    });
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self right];
    self.MYXHttpManag =[[MYXHttpManager alloc]init];
    [self initrandtagsRequest];
     WS(weakSelf)
    
    _channelView = [[XLChannelView alloc]initWithFrame:self.view.bounds];
    if (self.userinfoMode.tags.length>0) {
        NSArray *array = [self.userinfoMode.tags componentsSeparatedByString:@","];
        if (array.count>0) {
           // [_channelView.inUseTitles removeAllObjects];
            _channelView.inUseTitles = [array mutableCopy];
            
        }
       
    }
     [self channel];
   
   // _channelView.inUseTitles = [NSMutableArray arrayWithArray:arr1];
    //_channelView.unUseTitles = [NSMutableArray arrayWithArray:arr2];
    __weak XLChannelView * weakchannelView = _channelView;
    _channelView.subTGes =^(NSInteger tag){
        if (tag-100==0) {
            if (_channelView.inUseTitles.count == 5) {
                [weakSelf showNoReceivertext:@"最多只能选择5个标签"];
                return;
            }
            [weakSelf channel];
            MYXInterestViewController *MYXInterestVC =[[MYXInterestViewController alloc]init];
            MYXInterestVC.InterestBlock =^(NSString * Interest){
                GRLog(@"--Interest-%@",Interest);
                if (Interest.length>0) {
                     [weakchannelView.inUseTitles addObject:Interest];
                }
                [weakSelf channel];
                
            };
            MYXInterestVC.navigationItem.title= @"我的兴趣";
            [weakSelf.navigationController pushViewController:MYXInterestVC animated:YES];
        }else if (tag-100==1){
            [weakSelf initrandtagsRequest];
        }
    };
    [self.view addSubview: _channelView];
}


-(void)initrandtagsRequest{
    [self.MYXHttpManag randtags:^(NSArray *randtagsListArray) {
        if (randtagsListArray.count>0) {
            [self.randtagsArr removeAllObjects];
            [self.randtagsArr addObjectsFromArray:randtagsListArray];
            [self cutCopyItemsWithArray:self.randtagsArr
             ];
            _channelView.unUseTitles = self.randtagsArr;
             [_channelView reloadData];
        }
        
    } failure:^(NSString *errorCode) {
         [JXTHUDHelper showHUDText:errorCode type:@"error" inView:nil];
    } netDown:^{
          [JXTHUDHelper showHUDText:@"网络出错了～" type:@"error" inView:nil];
    }];
}

-(void)channel{
   // _channelView.inUseTitles = [self.LabelArr mutableCopy];
    [_channelView reloadData];
    
}

#pragma mark - 没有输入任何内容的提示
- (void)cutCopyItemsWithArray:(NSMutableArray *)mArray{
    NSLog(@"before array%@",mArray);

        for (NSString *item  in _channelView.inUseTitles) {
            for (NSInteger i = 0; i < mArray.count; i++) {
                NSString *netItem = mArray[i];
            if ([netItem isEqualToString:item]) {
                [mArray removeObjectAtIndex:i];
            }
            }
        }
    NSLog(@"after array%@",mArray);

}
-(void)showNoReceivertext:(NSString *)message
{
    [JXTHUDHelper showHUDText:message type:@"warning" inView:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
